#include "ImageDataSelect.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char ** argv)
{
    printf("ImageDataSelect----in----\n");
    enum Dog
    {
        AkitaDog,
        BorderCollie,
        Chihuahua,
        Chow,
        Dachshund,
        EnglishBulldog,
        GermanShepherd,
        GoldenRetriever,
        Huskies,
        Pomeranian,
        TibetanMastiff
    };
    char* everyDogDirName[10] = 
    {
        "AkitaDog",
        "BorderCollie",
        "Chihuahua",
        "Chow",
        "Dachshund",
        "EnglishBulldog",
        "GermanShepherd",
        "GoldenRetriever",
        "Huskies",
        "Pomeranian",
    };
    char dogDir[256] = "/home/cgf/Desktop/GitHub/DLEngine/ImageSource/Dogs/random";
    char nowDir[10][256];
    for(int i = 0; i < 10; ++i)
    {
        sprintf(nowDir[i], "%s/%s/28x28_gray", dogDir, everyDogDirName[i]);
        printf("%s\n", nowDir[i]);
    }
    int totalData = 12000;
    int classData  = 10;
    int everyData = totalData / classData;
    int classStillData[10];
    int selectIndex = 0;
    for(int i = 0; i < classData; ++i)
    {
        classStillData[i] = 0;
    }
    
    srand(time(NULL));

    char dataFileName[256] = "./data.data";
    char labelFileName[256] = "./label.data";
    remove(dataFileName);
    remove(labelFileName);
    FILE* fpData = fopen(dataFileName, "ab");
    FILE* fpLabel = fopen(labelFileName, "ab");
    
    char data[28][28];
    if(fpData == NULL||fpLabel == NULL)
    {
        return 0;
    }
    for(int i = 0; i < 16; ++i)
    {
        fputc(i, fpData);
    }
    for(int i = 0; i < 8; ++i)
    {
        fputc(i, fpLabel);
    }
    for(int i = 0; i < totalData; ++i)
    {
        selectIndex = rand() % 10;
        printf("select index:%d\n", selectIndex);
        classStillData[selectIndex] ++;
        char fpEveryDataFile[256];
        int everySingleIndex = classStillData[selectIndex];
        FILE* fpEveryData = NULL;
        do
        {
            sprintf(fpEveryDataFile, "%s/%d.jpg", nowDir[selectIndex], everySingleIndex);
            fpEveryData = fopen(fpEveryDataFile, "rb");
            everySingleIndex++;
            //printf("open %s to read\n", fpEveryDataFile);
        } while(fpEveryData == NULL);

        printf("open %s to read\n", fpEveryDataFile);
        fread(data, sizeof(char), 28*28, fpEveryData);
        if(fpEveryData != NULL)
        {
            fclose(fpEveryData);
        }
        fwrite(data, sizeof(char), 28*28, fpData);
        fwrite(&selectIndex, sizeof(char), 1, fpLabel);
    }

    if(fpData != NULL)
      fclose(fpData);
    if(fpLabel != NULL)
      fclose(fpLabel);

    printf("-----------------------------------------------------\n");
    for(int i = 0; i < classData; ++i)
    {
        printf("classStillData[%d] = %d\n", i, classStillData[i]);
    }
    printf("-----------------------------------------------------\n");
    return 0;
}
